import {
	coordinate
} from '@/utils/distance.js' // 获取定位
import {
	http
} from '@/utils/http.js'
// // extensions 气象类型
// const getWeather = (extensions) => {
// 	return new Promise((resolve, reject) => {
// 		uni.request({
// 			method: 'GET',
// 			url: 'https://restapi.amap.com/v3/weather/weatherInfo', //仅为示例，并非真实接口地址。
// 			data: {
// 				extensions: extensions || 'base',
// 				key: 'c58c741b1d20e8df2308c7f37ea59fbf',
// 				city: 430104
// 			},
// 			success: (res) => {
// 				resolve(res.data.lives)
// 			},
// 			fail: (err) => {
// 				reject(err)
// 			}
// 		});
// 	})
// }
const getWeather = async (extensions) => {
	const jwd = await coordinate() // 获取定位位置
	const obj = {
		longitude: jwd.longitude,
		latitude: jwd.latitude
	}
	const {
		data: res
	} = await http(false, 'http://119.29.110.86/map/gaode/reverseAddress', obj, 'POST')
	if (res.code !== '0000') {
		return uni.showToast({
			title: '查询天气失败',
			icon: 'none',
			position: 'top'
		});
	} else {
		const obj2 = {
			city: res.data.adCode || 430104,
			extensions: extensions || 'base'
		}
		const {
			data: res2
		} = await http(false, 'http://119.29.110.86/map/gaode/weatherInfo', obj2, 'POST')
		return new Promise((resolve, reject) => {
			// reject(err);
			if (res2.code !== '0000') {
				return uni.showToast({
					title: '查询天气失败',
					icon: 'none',
					position: 'top'
				});
			} else {
				return resolve(res2.data);
			}
		})
	}

}

export {
	getWeather
}
